// pages/payorder/index.js
import {
  Index
} from 'index-model.js'
var ajax = new Index(); // 实例化
Page({

  /**
   * 页面的初始数据
   */
  data: {
    payOrderInfo:[], // 支付订单信息
    orderData:[]     // 支付订单信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let OrderItem = wx.getStorageSync("CarOrderData");
    var item = OrderItem.OrderDtos;
    let list= [];
    for(let i=0;i<item.length;i++){
      list.push({ "Id": item[i].Id, "OrderPrice":item[i].OrderPrice,"Type":item[i].Type});
    }

    this.setData({
      payOrderInfo: OrderItem,
      orderData: list
    })
  },

  // 微信支付
  wxChatPayMnet:function(e){
    let that = this;
    let userid = 'hnacyb';
    let orderType =  wx.getStorageSync('orderStatusType');
    let menter = {
      PayType:0,
      OrderDtos: that.data.orderData,
      AppId:userid,
      OrderType:orderType
    };
    ajax.getPayMentData(menter,(res)=>{
      if (res.status == 1){
        that.requestPayment(res.data)
      }else{
        wx.showModal({
          title: '提示',
          showCancel:false,
          content: res.message,
          success (res) {
            if (res.confirm) {
              wx.redirectTo({
                url: '/pages/index/index',
              })
            } else if (res.cancel) {
              wx.redirectTo({
                url: '/pages/index/index',
              })
            }
          }
        })
      }
    })
  },

  //小程序支付调用
  requestPayment:function(res){
    let item = res.MiniProgramPayResponse;
    wx.requestPayment({
      timeStamp: item.TimeStamp,
      nonceStr: item.NonceStr,
      package: item.Package,
      signType: 'MD5',
      paySign: item.Sign,
      success(res) {
        wx.removeStorageSync('orderStatusType');
        wx.removeStorageSync('pathUrl');
        //console.log(res)
        wx.showToast({
          title: '支付成功',
          icon: 'success',
          duration: 1500,
          success:function(res){
            setTimeout(function(){
              wx.redirectTo({
                url: '/pages/index/index'
              })
            },1500)
          }
        })
      },
      fail(err) {
        wx.showToast({
          title: err.message,
          icon: 'none',
          duration: 1000
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})